﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using DAL.ADO.Facades;
using GrayParrot.Data.ADONET;
using Test.Dati.Libs;

namespace Test.Dati
{
    public class OrderDetailAggregateRoot
    {
        public int? OrderId { get; set; }
        public int? ProductId { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var factory = new DbConnectionFactory("NorthWindCn");
            DbContext context1 = new DbContext(factory);
            DAL.ADO.Repositories.CategoryRepository rep = new DAL.ADO.Repositories.CategoryRepository(context1);
            Category objs2 = rep.Find(new DAL.ADO.Utilities.CategoryAggregateRoot() { CategoryId = 1 });
            Console.WriteLine(objs2.CategoryName);

            //MyDbContext ctxPP = new MyDbContext();
            //GrayParrot.Data.EntityFramework.Repository<OrderDetail, OrderDetailAggregateRoot> pppp
            //    = new GrayParrot.Data.EntityFramework.Repository<OrderDetail, OrderDetailAggregateRoot>(ctxPP);

            //OrderDetailAggregateRoot proot = new OrderDetailAggregateRoot() { OrderId = 10248,  ProductId = 11 };
            //OrderDetail m_od = pppp.Find(proot);

            //Console.WriteLine(m_od);

            //var factory = new DbConnectionFactory("NorthWindCn");

            //// during start of the current session
            //DbContext context1 = new DbContext(factory);

            //using (var uow1 = context1.CreateUnitOfWork())
            //{
            //    // esempio di due facade con transazione...
            //    using (CustomerFacade m_custFac1 = CustomerFacade.Create(context1))
            //    {
            //        Customer m_customer3 = m_custFac1.Repository.Find(x => x.CustomerId == "ALFKI").FirstOrDefault();
            //        m_customer3.City = "Bari9";
            //        m_custFac1.Repository.Update(m_customer3);
            //    }

            //    using (CustomerFacade m_custFac2 = CustomerFacade.Create(context1))
            //    {
            //        Customer m_customer4 = m_custFac2.Repository.Find(x => x.CustomerId == "ANATR").FirstOrDefault();
            //        m_customer4.City = "Brindisi9";
            //        m_custFac2.Repository.Update(m_customer4);
            //    }

            //    uow1.SaveChanges();
            //}




            /* EF */
            GrayParrot.Data.EntityFramework.DbContext<MyDbContext> m_ctx = new GrayParrot.Data.EntityFramework.DbContext<MyDbContext>();

            try
            {
                using (var uow2 = m_ctx.CreateUnitOfWork())
                {
                    using (CustomerEFFacade m_customerFacEF = CustomerEFFacade.Create(m_ctx.BuildContext()))
                    {
                        Customer m_customerEF1 = m_customerFacEF.Repository.Find("ALFKI");

                        m_customerEF1.City = "Bari11";
                        m_customerFacEF.Repository.Update(m_customerEF1);
                    }

                    using (CustomerEFFacade m_customerFacEF2 = CustomerEFFacade.Create(m_ctx.BuildContext()))
                    {
                        Customer m_customerEF2 = m_customerFacEF2.Repository.Find("ANATR");

                        m_customerEF2.City = "Brindisi10";
                        m_customerFacEF2.Repository.Update(m_customerEF2);

                        int s = 10;
                        int b = 0;
                        //int c = s / b;
                    }

                    uow2.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
}
